package com.zzx.threekingdom.judge;

import com.zzx.threekingdom.judge.Judge.IAskYesNo;
import com.zzx.threekingdom.judge.Judge.IPassAction;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.ActionInfo;
import com.zzx.threekingdom.model.Damage;

public class AskOtherEach extends AskAll
{
	private String needActionName = null;
	public void beginAsk(ActionInfo act, Runnable doneR)
	{
		actionInfo.clear();
		setDone(doneR);
		
		if(act.getActionName().equals("南蛮入侵") == true)
			needActionName = "杀";
		else if(act.getActionName().equals("万箭齐发 ") == true)
			needActionName = "闪";
		else if(needActionName==null)
		{
			endAsk();
			return;
		}
	
		actionInfo.setActionName(act.getActionName());
		actionInfo.setFrom(act.getFrom());
		actionInfo.setCard(act.getCard());
		beginAsk(Judge.judge.getLivePmFromeMe(act.getFrom()), doneR);
	}

	@Override
	public void doAsk(final AbsPlayerModel pm, Runnable doneR)
	{
		IAskYesNo iAskYesNo = new IAskYesNo() { public void onResult(boolean yes) {
			if(yes == false)
			{
				final ActionInfo damageAct = new ActionInfo(actionInfo.getFrom(), pm, actionInfo.getActionName(), actionInfo.getCard());
				
				IPassAction ipa = new IPassAction() { public void onGotAction(ActionInfo act) {
					if(!(act != null && act.getActionName().equals(needActionName)))
					{
						pm.onDamage(new Damage(damageAct), new Runnable() { public void run() { goOnAsk(); }});
					}
					else
						goOnAsk();
				}};
				
				pm.onNeedActionByName(needActionName, ipa, damageAct);
			}
			else
				goOnAsk();
		}};
		
		new AskImpeccable().beginAsk(actionInfo.getFrom(), actionInfo.getActionName(), pm, iAskYesNo);
	}
	
}
